
cap log close
log using "$log/09_AWFP_data_prep_${S_DATE}.log", replace

use "$AWFP2\pq207", clear // stock
drop st_male

keep if mod(q,4) == 0
gen byte a = ceil(q/4)
drop q

merge 1:1 betnr a using  "$AWFP2\pa307" // inflows std. definition
drop in_male
drop _merge

merge 1:1 betnr a using  "$AWFP2\pa407" // outflows std. definition
drop out_male
drop _merge

foreach var of varlist _all {
	replace `var' = 0 if missing(`var')
}

gen year = 1974 + a
su year

keep if inrange(year,1997,2018)

drop a


global windows "w_3_stack" 

********************************************************************************
* Merge in chosen AKM-Values
********************************************************************************

gen ffe = .
gen re = .
gen cod = .

	if "$windows"=="w_3_stack" {
		forval t = 1999(3)2017 {	
			merge m:1 betnr using "$FE//firmfe_value_new_ver`t'3.dta", keep(1 3) nogen
			replace ffe = firm_fe if year>=`t'-1 & year<=`t'+1
			replace re = rent if year>=`t'-1 & year<=`t'+1
			replace cod = cd if year>=`t'-1 & year<=`t'+1
			drop firm_fe rent cd
		}		
	}

rename ffe firm_fe
rename re rents
rename cod cd

*******************************************************************************

drop V value std_fe std_V value_p
drop firm_fe rents

* Current establishment: Merge ind and bula
merge m:1 betnr using "$temp/wz_bula", keepusing(ind bula)
drop if _merge==2
drop _merge 

bysort year: egen median_cd = median(cd)

gen high_cd = cd >= median_cd

xtset betnr year
gen lag_st_eop = l.st_eop
replace lag_st_eop = 0 if missing(lag_st_eop)

keep if inrange(year,1998,2018)
keep if !missing(cd)

tabstat st_eop, by(high_cd)

* Generate rates etc.
gen denominator = (st_eop + lag_st_eop) / 2
gen egr = (st_eop - lag_st_eop) / denominator
gen einr  = in_eop  / denominator
gen eoutr = out_eop / denominator

compress
save "$temp\AWFP_test", replace


cap log close
log using "$log/13_AWFP_results_${S_DATE}.log", replace

use "$temp\AWFP_test", clear

gen byte high_am = 1 - high_cd

*** USE u-rate (removing trainees cohorts used in analysis)
rename year year_entry
rename bula region_entry
merge m:1 year_entry region_entry using "$temp\U_internal.dta", keepusing(U_internal)
rename year_entry year
rename region_entry bula
drop if _merge == 2
drop _merge

rename U_internal U_internal_t
replace year = year - 1 
rename year year_entry
rename bula region_entry
merge m:1 year_entry region_entry using "$temp\U_internal.dta", keepusing(U_internal)
rename year_entry year
rename region_entry bula
drop if _merge == 2
drop _merge
replace year = year + 1 
rename U_internal U_internal_lag
rename U_internal_t U_internal

gen delta_u = U_internal - U_internal_lag
replace delta_u = delta_u / 100
su delta_u
su U_internal
su year

********************************************************************************

local erates = "egr einr eoutr"

corr `erates' delta_u
bysort high_cd: corr `erates' delta_u

foreach var of varlist `erates' {
reg `var' c.delta_u##i.high_am, vce(cluster bula)
reghdfe `var' c.delta_u##i.high_am, absorb(ind) vce(cluster bula)
reghdfe `var' c.delta_u##i.high_am, absorb(betnr) vce(cluster bula)
}

bysort high_cd: corr `erates' delta_u [aweight = denominator]

foreach var of varlist `erates' {
reg `var' c.delta_u##i.high_am [aweight = denominator], vce(cluster bula)
reghdfe `var' c.delta_u##i.high_am [aweight = denominator], absorb(ind) vce(cluster bula)
reghdfe `var' c.delta_u##i.high_am [aweight = denominator], absorb(betnr) vce(cluster bula)
}

local varlist = "egr einr eoutr st_eop denominator"
foreach var of varlist `varlist' {
su `var', de // AWFP overall
su `var' if !missing(cd), de // Our subsample - connected set - with cd
}

tab bula [aweight = denominator]
tab bula [aweight = denominator] if !missing(cd)

gen ind2 = floor(ind/10)
gen ind1 = floor(ind2/10)

tab ind1 [aweight = denominator]
tab ind1 [aweight = denominator] if !missing(cd)

tab ind2 [aweight = denominator]
tab ind2 [aweight = denominator] if !missing(cd)

********************************************************************************


cap drop U_internal U_internal_lag delta_u

*** USE standard u-rate (all workers)
rename year year_entry
rename bula region_entry
merge m:1 year_entry region_entry using "$temp\U_internal_all_workers.dta",keepusing(U_internal)
rename year_entry year
rename region_entry bula
drop if _merge == 2
drop _merge

rename U_internal U_internal_t
replace year = year - 1 
rename year year_entry
rename bula region_entry
merge m:1 year_entry region_entry using "$temp\U_internal_all_workers.dta", keepusing(U_internal)
rename year_entry year
rename region_entry bula
drop if _merge == 2
drop _merge
replace year = year + 1 
rename U_internal U_internal_lag
rename U_internal_t U_internal

gen delta_u = U_internal - U_internal_lag
replace delta_u = delta_u / 100
su delta_u
su U_internal

su year

local erates = "egr einr eoutr" 
corr `erates' delta_u
bysort high_cd: corr `erates' delta_u

foreach var of varlist `erates' {
reg `var' c.delta_u##i.high_am, vce(cluster bula)
reghdfe `var' c.delta_u##i.high_am, absorb(ind) vce(cluster bula)
reghdfe `var' c.delta_u##i.high_am, absorb(betnr) vce(cluster bula)
}

bysort high_cd: corr `erates' delta_u [aweight = denominator]

foreach var of varlist `erates' {
reg `var' c.delta_u##i.high_am [aweight = denominator], vce(cluster bula)
reghdfe `var' c.delta_u##i.high_am [aweight = denominator], absorb(ind) vce(cluster bula)
reghdfe `var' c.delta_u##i.high_am [aweight = denominator], absorb(betnr) vce(cluster bula)
}

********************************************************************************

corr st_eop cd
corr st_eop cd [aweight = denominator]

clear
cap log close

